package com.criticalbotch.ijnition;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;

import org.json.JSONObject;

import com.criticalbotch.ijnition.checkers.UserChecker;
import com.google.gson.Gson;

public class UserManager {

	private HashMap<Integer, EnjinUser> users = new HashMap<Integer, EnjinUser>();

	private long timeLastChecked;
	private int checkPeriod = 300000; // 5 minutes
	private iJnition core;

	public UserManager() {
		checkUsers();
	}

	public UserManager(iJnition core) {
		setCore(core);
		checkUsers();
	}

	public UserManager(iJnition core, int checkperiod) {
		setCheckPeriod(checkPeriod);
		setCore(core);
		checkUsers();
	}

	private void checkUsers() {
		UserChecker checker = new UserChecker(getCore().getBaseURL()
				+ "/api/get-users", this, getCheckPeriod());
		checker.start();
		setTimeLastChecked(System.currentTimeMillis());
	}

	private int getCheckPeriod() {
		return checkPeriod;
	}

	private iJnition getCore() {
		return core;
	}

	EnjinUser getUser(String name) {
		EnjinUser retVal = null;

		HashMap<Integer, EnjinUser> userList = usersFromJSONFile("get-users.json");

		for (int i : userList.keySet()) {
			if (name.equals(userList.get(i).getUsername()))
				retVal = userList.get(i);
		}

		return retVal;
	}

	EnjinUser getUser(int ID) {
		EnjinUser retVal = null;

		HashMap<Integer, EnjinUser> userList = usersFromJSONFile("get-users.json");

		if (userList.containsKey(ID))
			retVal = userList.get(ID);

		return retVal;
	}

	public HashMap<Integer, EnjinUser> getUsers() {
		return users;
	}

	void loadUsers() {
		setUsers(usersFromJSONFile(core.getBaseURL() + "/api/get-users"));
	}

	public void notifiedUserCheck() {
		checkUsers();
	}

	public void setCheckPeriod(int t) {
		checkPeriod = t;
	}

	private void setCore(iJnition core) {
		this.core = core;
	}

	private void setTimeLastChecked(long t) {
		timeLastChecked = t;
	}

	void setUsers(HashMap<Integer, EnjinUser> users) {
		this.users = users;
	}

	public HashMap<Integer, EnjinUser> usersFromJSONFile(String s) {
		return usersFromJSONFile(new File(s));
	}

	public HashMap<Integer, EnjinUser> usersFromJSONFile(File f) {

		HashMap<Integer, EnjinUser> userList = new HashMap<Integer, EnjinUser>();

		try {
			Gson gson = new Gson();

			BufferedReader reader = new BufferedReader(new FileReader(f));

			String json = "";
			String line;

			while ((line = reader.readLine()) != null)
				json += line;

			reader.close();

			JSONObject job = new JSONObject(json);

			for (Object id : job.keySet()) {
				String ID = id.toString();
				int userID = Integer.parseInt(ID);
				EnjinUser user = gson.fromJson(job.get(ID).toString(),
						EnjinUser.class);
				user.setID(userID);
				userList.put(userID, user);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return userList;
	}

}
